// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import gnu.trove.map.hash.THashMap;
import java.util.ArrayList;
import se.krka.kahlua.j2se.KahluaTableImpl;
import se.krka.kahlua.vm.KahluaTableIterator;
import zombie.core.Core;
import zombie.core.math.PZMath;
import zombie.core.textures.Texture;
import zombie.iso.sprite.IsoSprite;
import zombie.iso.sprite.IsoSpriteInstance;
import zombie.iso.sprite.IsoSpriteManager;
import zombie.network.GameServer;
import zombie.util.LocationRNG;
import zombie.util.StringUtils;

public class TileOverlays {
    public static final TileOverlays instance = new TileOverlays();
    private static final THashMap<String, TileOverlays.TileOverlay> overlayMap = new THashMap<>();
    private static final ArrayList<TileOverlays.TileOverlayEntry> tempEntries = new ArrayList();

    public void addOverlays(KahluaTableImpl kahluaTableImpl0) {
        KahluaTableIterator kahluaTableIterator0 = kahluaTableImpl0.iterator();

        while (kahluaTableIterator0.advance()) {
            String string0 = kahluaTableIterator0.getKey().toString();
            if (!"VERSION".equalsIgnoreCase(string0)) {
                TileOverlays.TileOverlay tileOverlay = new TileOverlays.TileOverlay();
                tileOverlay.tile = string0;
                KahluaTableImpl kahluaTableImpl1 = (KahluaTableImpl)kahluaTableIterator0.getValue();
                KahluaTableIterator kahluaTableIterator1 = kahluaTableImpl1.iterator();

                while (kahluaTableIterator1.advance()) {
                    KahluaTableImpl kahluaTableImpl2 = (KahluaTableImpl)kahluaTableIterator1.getValue();
                    TileOverlays.TileOverlayEntry tileOverlayEntry = new TileOverlays.TileOverlayEntry();
                    tileOverlayEntry.room = kahluaTableImpl2.rawgetStr("name");
                    tileOverlayEntry.chance = kahluaTableImpl2.rawgetInt("chance");
                    tileOverlayEntry.usage.parse(kahluaTableImpl2.rawgetStr("usage"));
                    KahluaTableImpl kahluaTableImpl3 = (KahluaTableImpl)kahluaTableImpl2.rawget("tiles");
                    KahluaTableIterator kahluaTableIterator2 = kahluaTableImpl3.iterator();

                    while (kahluaTableIterator2.advance()) {
                        String string1 = kahluaTableIterator2.getValue().toString();
                        if (StringUtils.isNullOrWhitespace(string1) || "none".equalsIgnoreCase(string1)) {
                            string1 = "";
                        } else if (Core.bDebug && !GameServer.bServer && Texture.getSharedTexture(string1) == null) {
                            System.out.println("BLANK OVERLAY TEXTURE. Set it to \"none\".: " + string1);
                        }

                        tileOverlayEntry.tiles.add(string1);
                    }

                    tileOverlay.entries.add(tileOverlayEntry);
                }

                overlayMap.put(tileOverlay.tile, tileOverlay);
            }
        }
    }

    public boolean hasOverlays(IsoObject obj) {
        return obj != null && obj.sprite != null && obj.sprite.name != null && overlayMap.containsKey(obj.sprite.name);
    }

    public void updateTileOverlaySprite(IsoObject obj) {
        if (obj != null) {
            IsoGridSquare isoGridSquare = obj.getSquare();
            if (isoGridSquare != null) {
                String string0 = null;
                float float0 = -1.0F;
                float float1 = -1.0F;
                float float2 = -1.0F;
                float float3 = -1.0F;
                if (obj.sprite != null && obj.sprite.name != null) {
                    TileOverlays.TileOverlay tileOverlay = overlayMap.get(obj.sprite.name);
                    if (tileOverlay != null) {
                        String string1 = "other";
                        if (isoGridSquare.getRoom() != null) {
                            string1 = isoGridSquare.getRoom().getName();
                        }

                        TileOverlays.TileOverlayEntry tileOverlayEntry = tileOverlay.pickRandom(string1, isoGridSquare);
                        if (tileOverlayEntry == null) {
                            tileOverlayEntry = tileOverlay.pickRandom("other", isoGridSquare);
                        }

                        if (tileOverlayEntry != null) {
                            if (tileOverlayEntry.usage.bTableTop && this.hasObjectOnTop(obj)) {
                                return;
                            }

                            string0 = tileOverlayEntry.pickRandom(isoGridSquare.x, isoGridSquare.y, isoGridSquare.z);
                            if (tileOverlayEntry.usage.alpha >= 0.0F) {
                                float2 = 1.0F;
                                float1 = 1.0F;
                                float0 = 1.0F;
                                float3 = tileOverlayEntry.usage.alpha;
                            }
                        }
                    }
                }

                if (!StringUtils.isNullOrWhitespace(string0) && !GameServer.bServer && Texture.getSharedTexture(string0) == null) {
                    string0 = null;
                }

                if (!StringUtils.isNullOrWhitespace(string0)) {
                    if (obj.AttachedAnimSprite == null) {
                        obj.AttachedAnimSprite = new ArrayList(4);
                    }

                    IsoSprite isoSprite = IsoSpriteManager.instance.getSprite(string0);
                    isoSprite.name = string0;
                    IsoSpriteInstance isoSpriteInstance = IsoSpriteInstance.get(isoSprite);
                    if (float3 > 0.0F) {
                        isoSpriteInstance.tintr = float0;
                        isoSpriteInstance.tintg = float1;
                        isoSpriteInstance.tintb = float2;
                        isoSpriteInstance.alpha = float3;
                    }

                    isoSpriteInstance.bCopyTargetAlpha = false;
                    isoSpriteInstance.bMultiplyObjectAlpha = true;
                    obj.AttachedAnimSprite.add(isoSpriteInstance);
                }
            }
        }
    }

    private boolean hasObjectOnTop(IsoObject isoObject0) {
        if (!isoObject0.isTableSurface()) {
            return false;
        } else {
            IsoGridSquare isoGridSquare = isoObject0.getSquare();

            for (int _int = isoObject0.getObjectIndex() + 1; _int < isoGridSquare.getObjects().size(); _int++) {
                IsoObject isoObject1 = isoGridSquare.getObjects().get(_int);
                if (isoObject1.isTableTopObject() || isoObject1.isTableSurface()) {
                    return true;
                }
            }

            return false;
        }
    }

    public void fixTableTopOverlays(IsoGridSquare square) {
        if (square != null && !square.getObjects().isEmpty()) {
            boolean _boolean = false;

            for (int _int = square.getObjects().size() - 1; _int >= 0; _int--) {
                IsoObject isoObject = square.getObjects().get(_int);
                if (_boolean && isoObject.isTableSurface()) {
                    this.removeTableTopOverlays(isoObject);
                }

                if (isoObject.isTableSurface() || isoObject.isTableTopObject()) {
                    _boolean = true;
                }
            }
        }
    }

    private void removeTableTopOverlays(IsoObject isoObject) {
        if (isoObject != null && isoObject.isTableSurface()) {
            if (isoObject.sprite != null && isoObject.sprite.name != null) {
                if (isoObject.AttachedAnimSprite != null && !isoObject.AttachedAnimSprite.isEmpty()) {
                    TileOverlays.TileOverlay tileOverlay = overlayMap.get(isoObject.sprite.name);
                    if (tileOverlay != null) {
                        int int0 = isoObject.AttachedAnimSprite.size();

                        for (int int1 = 0; int1 < tileOverlay.entries.size(); int1++) {
                            TileOverlays.TileOverlayEntry tileOverlayEntry = (TileOverlays.TileOverlayEntry)tileOverlay.entries.get(int1);
                            if (tileOverlayEntry.usage.bTableTop) {
                                for (int int2 = 0; int2 < tileOverlayEntry.tiles.size(); int2++) {
                                    this.tryRemoveAttachedSprite(isoObject.AttachedAnimSprite, (String)tileOverlayEntry.tiles.get(int2));
                                }
                            }
                        }

                        if (int0 != isoObject.AttachedAnimSprite.size()) {
                        }
                    }
                }
            }
        }
    }

    private void tryRemoveAttachedSprite(ArrayList<IsoSpriteInstance> arrayList, String string) {
        for (int _int = 0; _int < arrayList.size(); _int++) {
            IsoSpriteInstance isoSpriteInstance = (IsoSpriteInstance)arrayList.get(_int);
            if (string.equals(isoSpriteInstance.getName())) {
                arrayList.remove(_int--);
                IsoSpriteInstance.add(isoSpriteInstance);
            }
        }
    }

    public void Reset() {
        overlayMap.clear();
    }

    private static final class TileOverlay {
        public String tile;
        public final ArrayList<TileOverlays.TileOverlayEntry> entries = new ArrayList();

        public void getEntries(String string, IsoGridSquare isoGridSquare, ArrayList<TileOverlays.TileOverlayEntry> arrayList) {
            arrayList.clear();

            for (int _int = 0; _int < this.entries.size(); _int++) {
                TileOverlays.TileOverlayEntry tileOverlayEntry = (TileOverlays.TileOverlayEntry)this.entries.get(_int);
                if (tileOverlayEntry.room.equalsIgnoreCase(string) && tileOverlayEntry.matchUsage(isoGridSquare)) {
                    arrayList.add(tileOverlayEntry);
                }
            }
        }

        public TileOverlays.TileOverlayEntry pickRandom(String string, IsoGridSquare isoGridSquare) {
            this.getEntries(string, isoGridSquare, TileOverlays.tempEntries);
            if (TileOverlays.tempEntries.isEmpty()) {
                return null;
            } else {
                int _int = LocationRNG.instance.nextInt(TileOverlays.tempEntries.size(), isoGridSquare.x, isoGridSquare.y, isoGridSquare.z);
                return (TileOverlays.TileOverlayEntry)TileOverlays.tempEntries.get(_int);
            }
        }
    }

    private static final class TileOverlayEntry {
        public String room;
        public int chance;
        public final ArrayList<String> tiles = new ArrayList();
        public final TileOverlays.TileOverlayUsage usage = new TileOverlays.TileOverlayUsage();

        public boolean matchUsage(IsoGridSquare isoGridSquare) {
            return this.usage.match(isoGridSquare);
        }

        public String pickRandom(int int1, int int2, int int3) {
            int int0 = LocationRNG.instance.nextInt(this.chance, int1, int2, int3);
            if (int0 == 0 && !this.tiles.isEmpty()) {
                int0 = LocationRNG.instance.nextInt(this.tiles.size());
                return (String)this.tiles.get(int0);
            } else {
                return null;
            }
        }
    }

    private static final class TileOverlayUsage {
        String usage;
        int zOnly = -1;
        int zGreaterThan = -1;
        float alpha = -1.0F;
        boolean bTableTop = false;

        boolean parse(String string0) {
            this.usage = string0.trim();
            if (StringUtils.isNullOrWhitespace(this.usage)) {
                return true;
            } else {
                String[] string1 = string0.split(";");

                for (int _int = 0; _int < string1.length; _int++) {
                    String string2 = string1[_int];
                    if (string2.startsWith("z=")) {
                        this.zOnly = Integer.parseInt(string2.substring(2));
                    } else if (string2.startsWith("z>")) {
                        this.zGreaterThan = Integer.parseInt(string2.substring(2));
                    } else if (string2.startsWith("alpha=")) {
                        this.alpha = Float.parseFloat(string2.substring(6));
                        this.alpha = PZMath.clamp(this.alpha, 0.0F, 1.0F);
                    } else {
                        if (!string2.startsWith("tabletop")) {
                            return false;
                        }

                        this.bTableTop = true;
                    }
                }

                return true;
            }
        }

        boolean match(IsoGridSquare isoGridSquare) {
            return this.zOnly != -1 && isoGridSquare.z != this.zOnly ? false : this.zGreaterThan == -1 || isoGridSquare.z > this.zGreaterThan;
        }
    }
}
